import csv
from lxml import etree
import requests


# 爬取天气数据
def get_weather(url):
    weather_info = []  # 新建一个列表，将每个月的数据放进去
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.9",
        "Accept-Encoding": "gzip, deflate, br",
        "Connection": "keep-alive"
    }

    # 请求
    resp = requests.get(url, headers=headers)

    # 检查请求是否成功
    if resp.status_code != 200:
        print(f"请求失败，状态码: {resp.status_code}")
        return []

    # 数据预处理
    resp_html = etree.HTML(resp.text)

    # xpath提取所有数据
    resp_list = resp_html.xpath("//table[@class=\"b\"]//tr[position()>1]")

    # for循环迭代遍历


    for ls in resp_list:
        date = ls.xpath(".//td[1]//text()")  # 日期
        weather_day = ls.xpath(".//td[2]//text()")  # 白天天气状况
        temp_day = ls.xpath(".//td[3]//text()")  # 白天气温
        wind_day = ls.xpath(".//td[4]//text()")  # 白天风力

        # 处理数据（如有需要的话进行清洗或格式化）
        day_weather_info = {
            "date": ' '.join(date).strip(),
            "weather_day": ' '.join(weather_day).strip(),
            "temp_day": ' '.join(temp_day).strip(),
            "wind_day": ' '.join(wind_day).strip()
        }

        # 将每天的数据字典放入每月的数据列表中
        weather_info.append(day_weather_info)

    # 返回每个月的天气数据
    return weather_info


# 外层列表，年列表，里层列表，月列表，里层列表中的每个字典：这个月中每天的天气
weathers = []

# for循环生成有顺序的1-12
for month in range(1, 13):
    # 找url规律，获取对应月份的天气信息
    weather_time = f'2024{month:02}'
    url = f'http://www.tianqihoubao.com/lishi/tongling/month/{weather_time}.html'

    # 爬虫获取这个月的天气信息
    weather = get_weather(url)
    print(weather)

    # 存到列表中
    weathers.append(weather)

print(weathers)

# 数据写入（一次性写入）
with open("weather.csv", "w", newline='') as csvfile:
    writer = csv.writer(csvfile)

    # 先写入列名：columns_name
    writer.writerow(["日期", "天气状况", "最高/低温", "风向风力（白天/夜间）"])

    # 遍历出每个月的天气信息
    for month_weather in weathers:
        # 遍历出每天的天气信息
        for day_weather_dict in month_weather:
            # 将每天的天气信息写入csv文件
            writer.writerow(list(day_weather_dict.values()))

